<%@ tag import="java.util.List" %>
<%@ tag import="java.util.ArrayList" %>
<%@ tag pageEncoding="UTF-8"
        body-content="scriptless"
        description="分页"
%>

<%@ attribute name="pageNo" required="true" type="java.lang.Integer" description="pageNo" %>
<%@ attribute name="pageSize" required="true" type="java.lang.Integer" description="pageSize" %>
<%@ attribute name="total" required="true" type="java.lang.Integer" description="total" %>
<%@ attribute name="maxPage" required="true" type="java.lang.Integer" description="maxPage" %>
<%@ attribute name="maxGroup" required="false" type="java.lang.Integer" description="maxGroup" %>
<%
    // 传值传不过来，暂时定义一个值
    Integer max = 10;

    // 无值初始化分组数
    maxGroup = maxGroup == null ? 5 : maxGroup;

    List<String> pages = new ArrayList<>();
    pages.add(0, String.valueOf(pageNo));
    for (int i = 0; i < maxGroup; i++) {
        Integer last = pages.size() - 1;
        // 后分页
        if (i % 2 == 0) {
            Integer next = Integer.parseInt(pages.get(last)) + 1;
            if (next > max) {
                continue;
            } else {
                pages.add(pages.size(), String.valueOf(next));
            }
        }
        // 前分页
        if (i % 2 != 0) {
            Integer prev = Integer.parseInt(pages.get(0)) - 1;
            if (prev < 1) {
                continue;
            } else {
                pages.add(0, String.valueOf(prev));
            }
        }
    }

    // 增加首尾及···
    if (Integer.parseInt(pages.get(0)) - 2 > 1) {
        pages.add(0, "···");
        pages.add(0, "1");
    }
    if (Integer.parseInt(pages.get(pages.size() -1)) < max){
        pages.add(pages.size(), "···");
        pages.add(pages.size(), String.valueOf(max));
    }
    StringBuffer context = new StringBuffer("<div class='layui-btn-group' style='margin-left: 40%;'>");
	    pages.forEach(item -> {
	    String style = "";
	    if (item.equals(pageNo.toString())) {
	        style = "layui-border-orange";
	    } else {
	        style = "layui-border-blue";
	    }
	    context.append("<button data-pageNo='"+ item +"' class='page layui-btn layui-btn-primary " + style + "'>"+ item +"</button>");
	});
    context.append("</div>");
    out.write(context.toString());
%>